package com.water.camera.util;

import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.widget.Toast;

import me.drakeet.support.toast.ToastCompat;

public class ToastUtil {

    static ToastCompat toast;

    private static ToastCompat getToast(Context context, String content, int time) {
        ToastCompat toast = ToastCompat.makeText(context.getApplicationContext(), content, time);
        return toast;
    }

    public static void showLongToast(Context context, int id, int position) {
        if (context == null) {
            return;
        }
        String str = context.getString(id) + position;
        showToast(context, str, Toast.LENGTH_LONG);
    }

    public static void showLongToast(Context context, int id) {
        if (context == null) {
            return;
        }
        String str = context.getString(id);
        showToast(context, str, Toast.LENGTH_LONG);
    }

    public static void showLongToast(Context context, String content) {
        if (context == null) {
            return;
        }
        showToast(context, content, Toast.LENGTH_LONG);
    }

    public static void showShortToast(Context context, int id) {
        if (context == null) {
            return;
        }
        String str = context.getString(id);
        showToast(context, str, Toast.LENGTH_SHORT);
    }

    public static void showShortToast(Context context, int id, int g) {
        if (context == null) {
            return;
        }
        String str = context.getString(id);
        showToast(context, str, Toast.LENGTH_SHORT, g);
    }

    public static void showShortToast(Context context, int id, Gravity gravity) {
        if (context == null) {
            return;
        }
        String str = context.getString(id);
        showToast(context, str, Toast.LENGTH_SHORT);
    }

    public static void showShortToast(Context context, String content) {
        if (context == null || TextUtils.isEmpty(content)) {
            return;
        }
        showToast(context, content, Toast.LENGTH_SHORT);
    }

    public static void showShortToast(Context context, String content, int gravity) {
        showToast(context, content, Toast.LENGTH_SHORT, gravity);
    }

    private static void showToast(Context context, String content, int duration) {
        showToast(context, content, duration, Gravity.TOP);
    }

    private static void showToast(Context context, String content, int duration, int gravity) {
        if (context == null) {
            return;
        }
        try {
            if (toast != null) {
                toast.cancel();
            }
            toast = getToast(context, content, duration);
            toast.setGravity(gravity, 0, 0);
            toast.show();
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }
}
